import cTitle from "components/title";
// import { Toast } from 'vant';
import { scrollMixin } from "../../utils/mixin";
import BScroll from "better-scroll";
import couponCell from "./components/couponCell.vue"
import yzSubscribe from "components/ui_components/yz_subscribe";
import {bindDrainageScene} from "../../fun/modules/drainage_scene.js";

export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      selected: "",
      coupon_list: [],
      loading: false,
      allLoaded: false,
      goload: true,
      isLoadMore: true,
      page: 1,
      total_page: 0,
      // srcOne:require('../../assets/images/couponsbg_6@2x.png'),
      _status:1,
      types:[],
      typesIndex:0,
      slide_shows:[],
      isLoading:false,
      coupon_form:'',
      subTemplate:'',
      tabSticky:false,
      // 积分兑换
      isIntegralExchange: true,
      showIntegralPopup: false,
      integral_plugin_name: '',
      exchangeForm: {},
      couponScroll:"",
      tabIndex:0
    };
  },
  computed:{

  },
  methods: {
    initData() {
      this.coupon_list = [];
      this.page = 1;
      this.goload = true;
      this.total_page = 0;
      this.loading = true;
      this.allLoaded = false;
      this.isLoadMore = true;
      this.typesIndex = 0;
      this.tabIndex = 0;
    },
    //更新
    loadTop() {
      this.initData();
      this.getNetData();
      // this.$refs.loadmore.onTopLoaded();
    },
    toStore() {
      this.$router.push(this.fun.getUrl("coupon", {}));
    },
    initScroll() {
      if (this.types.length > 0) {
        this.couponScroll = new BScroll(this.$refs.handpickList, {
          scrollX: true,
          click: true
        });
      }
    },
    showCoupon() {
      this.$nextTick(() => {
        if (!this.couponScroll) {
          this.initScroll();
        } else {
          this.couponScroll.refresh();
        }
      });
    },
    setTypesIndex(ind,item){
      if(this.tabIndex == ind){
        return;
      }
      //酒店插件关闭后，会导致索引不对，搜索的商品列表错误
      this.typesIndex = item.id;
      this.tabIndex = ind;
      this.getNetData();
    },
    toRouteLink(link){
      if(link){
        window.location.href = link;
      }
    },

    // 积分兑换优惠券
    showIntegralExchange (item, index) {
      this.showIntegralPopup = true;
      this.selectItem = item;
      this.selectIndex = index;
      this.exchangeForm = {
        couponTotal: 1,
        exIntegral: item.exchange_coupon_integral
      };
    },

    exchangeCoupon () {
      this.showIntegralPopup = false;
      this.selectedcoupon(this.selectItem, this.selectIndex, this.exchangeForm.couponTotal);
    },

    selectedcoupon(item, index, exchangeTotal) {
      let isBindDrainageScene = bindDrainageScene({
        scene: 3,
        title: '领取优惠券'
      });
      if(!isBindDrainageScene) return;//爆客码绑定场景

      var that = this;
      let json = {
        coupon_id: item.id
      };
      // 积分兑换优惠劵
      if (item.isIntegralExchange && exchangeTotal > 0) {
        json.exchange_total = exchangeTotal;
      } else {
      //领取
        if(!that.fun.isTextEmpty(that.coupon_form)){
          that.$router.push(
            that.fun.getUrl("DiyForm", { id:that.coupon_form ,tag:'coupon'})
          );
          return;
        }
        //use_type==2为指定商品
        if(item.use_type == 2) {
          that.$router.push(
            that.fun.getUrl("CouponIndependent", { id:item.id })
          );
          return;
        }
      }
      $http
        .get(
          "coupon.member-coupon.get-coupon",
          json,
          "正在抢此优惠券"
        )
        .then(
          (response) => {
            if (response.result == 1) {
              var temp = response.data;
              temp.isIntegralExchange = (temp.is_integral_exchange_coupon != 0) && (temp.exchange_coupon_integral > 0);
              this.$set(that.coupon_list, index, temp);
              item.isIntegralExchange && exchangeTotal > 0?that.$toast({message: "兑换成功"}):that.$dialog.alert({message:"领取成功"});
            } else {
              if(response.data.reason){
                that.$dialog.alert({message:response.data.reason,title:response.msg});
              }else{
                that.$dialog.alert({message:response.msg});
              }

            }
          },
          function(response) {
            // error callback
          }
        );
    },
    //发起http请求
    getNetData() {
      var that = this;
      let json = { page: 1,coupon_type :that.typesIndex};
      $http
        .get(
          "coupon.member-coupon.coupons-for-member",
          json,
          "正在获取优惠券..."
        )
        .then(
          (response) => {
            if (response.result == 1) {
              let myData = response.data;
              let couponList = myData.data.data || [];
              couponList.forEach((item,index) => {
                item.isIntegralExchange = (item.is_integral_exchange_coupon != 0) && (item.exchange_coupon_integral > 0);
              })
              this.total_page = response.data.data.last_page;
              this.coupon_list = couponList;
              this.types = myData.search_array;
              this.slide_shows = myData.slide_shows;
              this.integral_plugin_name = myData.integral_plugin_name ? myData.integral_plugin_name : '消费积分';
              this.isLoading = false;
              this.showCoupon();
              if(myData.coupon_form) {
                this.coupon_form = myData.coupon_form
              }
            } else {
              this.$dialog.alert({message:response.msg});
            }
          },
          function(response) {
            // error callback
          }
        );
    },
    getMoreData(page) {
      var that = this;
      that.isLoadMore = false;
      console.log(this.page , this.total_page);
      if (this.page >= this.total_page) {
        return;
      }
      if (this.page >= this.total_page) {
        that.loading = true;
        that.allLoaded = true;
        return;
      } else {
        this.page = this.page + 1;
        $http
          .get(
            "coupon.member-coupon.coupons-for-member",
            {
              page: this.page,
              coupon_type :that.typesIndex,
            },
            "正在获取更多..."
          )
          .then(
            (response) => {
              if (response.result == 1) {
                var myData = response.data;
                let couponList = myData.data.data || [];
                that.loading = false;
                that.allLoaded = false;
                couponList.map((item,index) => {
                  item.isIntegralExchange = (item.is_integral_exchange_coupon != 0) && (item.exchange_coupon_integral > 0);
                })
                this.coupon_list = that.coupon_list.concat(couponList); //数组拼接
                this.isLoadMore = true;
                if(myData.coupon_form){
                  this.coupon_form = myData.coupon_form;
                }
              } else {
                this.page = this.page - 1;
                this.loading = true;
                this.allLoaded = true;
                this.isLoadMore = false;
                return;
              }
            },
            function(response) {
              // error callback
            }
          );
      }
    },
    confirmSub($event,item,index){
      this.selectedcoupon(item,index);
    },
    errorSub($event,item,index){
      console.log($event,item,index);
      this.selectedcoupon(item,index);
    }
  },
  activated() {
    this.coupon_list = [];
    this.initData();
    this.getNetData();
  },
  components: { yzSubscribe,cTitle,couponCell }
};
